<?php
    /**
     * Created by PhpStorm.
     * User: Fonny
     * Date: 14-8-27
     * Time: 下午5:07
     */

    $link = mysqli_connect("localhost", "root", "root");

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %d:\n%s"
            , mysqli_connect_errno()
            , mysqli_connect_error());
    }

//选择或创建数据库
    while (!mysqli_select_db($link, "php_study")) {
//    写入和读取之前必须用这一句更改提交的中文字符编码
        mysqli_query($link, "set names 'utf8'");
        //创建数据库
        $sql = "CREATE DATABASE php_study;";

        if (mysqli_query($link, $sql)) {
            echo "Database created";
            mysqli_select_db($link, "php_study");
//    写入和读取之前必须用这一句更改提交的中文字符编码
            mysqli_query($link, "set names 'utf8'");
            //创建表
            $sql = "CREATE TABLE tbl_user2 (
                  col_user_id int NOT NULL AUTO_INCREMENT,
                  col_user_name varchar(24) NOT NULL,
                  col_user_passwd blob NOT NULL,
                  PRIMARY KEY (col_user_id),
                  UNIQUE KEY col_user_name (col_user_name),
                  UNIQUE KEY col_user_id (col_user_id)
                ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";

            if (mysqli_query($link, $sql)) {
                echo("<p>Table created!</p>");
            } elseif (mysqli_errno($link) != 1050) {
                printf("<p>Table created Error code: %d</p><p>%s</p>"
                    , mysqli_errno($link)
                    , mysqli_error($link));
            }
        } else {
            printf("<p>Error creating database: %d</p><p>%s</p>"
                , mysqli_connect_errno()
                , mysqli_connect_error());
        }
    }
?>